﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using com.lover.astd.common.config;
using com.lover.astd.common.model;
using com.lover.astd.common;

namespace com.lover.astd.game.ui.server.impl.equipment
{
    public class MhServer : LogicServer
    {
        public MhServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_Mh; ServerReadableName = ConfigStrings.SR_Mh; }
        public override void renderSettings()
        {
            renderData();
            MainForm frm = _mainForm;
            Dictionary<string, string> conf = getConfig(ServerName);
            frm.chk_equip_mh_enable.Checked = conf.ContainsKey(ConfigStrings.enabled) && conf[ConfigStrings.enabled].ToLower().Equals("true");
            int fail_count_max = 0;
            if (conf.ContainsKey(ConfigStrings.fail_count)) Int32.TryParse(conf[ConfigStrings.fail_count], out fail_count_max);
            frm.num_mh_failcount.Value = fail_count_max;
            if (conf.ContainsKey(ConfigStrings.mh_ids)) renderDataGridToSelected<Equipment>(frm.dg_equipMh, conf[ConfigStrings.mh_ids]);
            if (conf.ContainsKey(ConfigStrings.mh_type) && conf[ConfigStrings.mh_type] != "")
            {
                frm.combo_mh_type.SelectedItem = conf[ConfigStrings.mh_type];
            }
            renderData();
        }
        public override void saveSettings()
        {
            MainForm frm = _mainForm;
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_equip_mh_enable.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.fail_count, frm.num_mh_failcount.Value.ToString());
            conf.setConfig(ServerName, ConfigStrings.mh_ids, getDataGridSelectedIds<Equipment>(frm.dg_equipMh));
            if (frm.combo_mh_type.SelectedItem != null)
            {
                conf.setConfig(ServerName, ConfigStrings.mh_type, frm.combo_mh_type.SelectedItem.ToString());
            }
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, "true");
            conf.setConfig(ServerName, ConfigStrings.mh_ids, "");
            conf.setConfig(ServerName, ConfigStrings.fail_count, "3");
            conf.setConfig(ServerName, ConfigStrings.mh_type, "少量"); //0=少量,1=大量,2=海量
            renderSettings();
        }
        
        private void renderData()
        {
            if (_mainForm.dg_equipMh.DataSource == null)
            {
                BindingSource src = new BindingSource();
                src.DataSource = getUser()._mhEquips;
                _mainForm.dg_equipMh.AutoGenerateColumns = false;
                _mainForm.dg_equipMh.DataSource = src;
            }
            _mainForm.dg_equipMh.Refresh();
        }
        private List<int> getEquipIdsToMh()
        {
            Dictionary<string, string> configs = getConfig(ServerName);
            string confstr = "";
            if (configs.ContainsKey("enchant_ids")) confstr = configs["enchant_ids"];
            return generateIds(confstr);
        }
    }
}
